/**
 *
 */
package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.action.result.ViewResult;
import org.whuims.pois.common.Chinese;
import org.whuims.pois.dao.ViewDao;
import org.whuims.pois.model.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author herochen
 * @date 2010-4-19 @time 上午11:08:01
 */
public class ViewAction extends DispatchAction {

    /**
     * 该类是基本的信息提取类，负责从主表whunews中取出数据，然后以List<ElementsBean>的形式返回
     * 该类继承了DispatchAction，提供多种方法： 1、最新新闻的查找 2、当天论坛信息的查找 3、当天博客信息的查找 4、给定关键词的查找
     * 5、大量相似ID的相似查找（SimilarAction）
     */
    private List<View> list = new ArrayList<View>();
    private List<ViewResult> groupList = new ArrayList<ViewResult>();

    private ViewDao dao = new ViewDao();

    /**
     * 查找最新观点
     */
    public ActionForward newest(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        long topicId = getTopic(request);
        if (topicId <= 0) {
            return mapping.findForward("login");
        }
        list = dao.getViews(topicId);

        request.setAttribute("result", list);
        request.setAttribute("topic", topicId);
        request.setAttribute("tag", "观点聚焦");
        return mapping.findForward("success");
    }


    /**
     * 该方法用于查找最新的新闻，取出最近的300条
     */
    public ActionForward viewgroup(ActionMapping mapping, ActionForm form,
                                   HttpServletRequest request, HttpServletResponse response) {
        String group = request.getParameter("group");
        long topicId = getTopic(request);
        if (topicId <= 0) {
            return mapping.findForward("login");
        }
        if (group == null || group.trim().equals("")) {
            group = "全部";
        }
        String showLabel = "";
        if (group.equals("person")) {
            showLabel = "人物";
        } else if (group.equals("newstitle")) {
            showLabel = "新闻";
        } else if (group.equals("newssite")) {
            showLabel = "站点";
        }
        groupList = dao.getViews(topicId, group);
        request.setAttribute("result", groupList);
        request.setAttribute("tag", group.equals("al") ? "观点聚焦" : "组别：<" + showLabel
                + ">");
        return mapping.findForward("success");
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return  (Long) topic;
    }

    public ActionForward getViewByGroupName(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        long topicId = getTopic(request);
        String groupType = request.getParameter("groupType");
        String groupName = request.getParameter("groupName");
        if (topicId <= 0) {
            return mapping.findForward("login");
        }
        list = dao.getViews(topicId);

        request.setAttribute("result", list);
        request.setAttribute("topic", topicId);
        request.setAttribute("tag", "观点聚焦");
        return mapping.findForward("success");
    }

    public ActionForward newest1(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response){
        long topicId = getTopic(request);
        String groupType = request.getParameter("groupType");
        String groupName1 = request.getParameter("groupName");
        String groupName = Chinese.toChinese(groupName1);
        List<View> list1 = dao.getViews(topicId);
        List<ViewResult> list2 = dao.getViewListByNewsInfo(groupType, list1);
        List<View> list3 = new ArrayList<View>();
        for(int i=0;i<list2.size();i++){
            if(list2.get(i).getGroupName().equals(groupName)){
                list3 = list2.get(i).getViewList();
                break;
            }
        }
        request.setAttribute("result", list3);
        request.setAttribute("tag", groupName);
        return mapping.findForward("success");
    }
}
